/**
 * LabWork class handles lab information
 */
package docoffice.doss;
import java.sql.ResultSet;
import java.util.logging.Logger;

import docoffice.database.DatabaseManager;

/**
 * @author Ray Nosari
 * @version $Revision 1.0
 */
public class LabWork {
	
	/**
	 *Field labWorkType. 
	 */
	private String workType;
	
	/**
	 * Field id.
	 */
	private int id; 
	
	/**
	 * Field cost.
	 */
	private double cost;
	
	/**
	 * Field LOG.
	 */
	private static final Logger LOG = Logger.getLogger("LabWork");
	
	/**
	 * Constructor for LabWork.
	 * @param workType String
	 * @param cost double
	 */
	public LabWork(String workType, double cost){
		this.workType = workType;
		this.cost = cost;
	}
	
	/**
	 * Method getWorkType.
	
	 * @return String */
	public String getWorkType(){
		return this.workType;
	}
	
	/**
	 * Method setWorkType.
	 * @param workType String
	 */
	public void setWorkType(String workType){
		this.workType  = workType;
	}
	
	/**
	 * Method getId.
	
	 * @return int */
	public int getId(){
		return this.id;
	}
	
	/**
	 * Method setId.
	 * @param id int
	 */
	public void setId(int id){
		this.id = id;
	}
	
	/**
	 * Method getCost.
	
	 * @return double */
	public double getCost(){
		return this.cost;
	}
	
	/**
	 * Method setCost.
	 * @param cost double
	 */
	public void setCost(double cost){
		this.cost = cost;
	}
	
	/**
	 * Method toString.
	
	 * @return String */
	public String toString(){
		return "this class contains lab info";
	}
	
	/**
	 * Method createLabWork.
	 * @param workType String
	 * @param cost String
	 */
	public void createLabWork(String workType, double cost){
		final DatabaseManager dbMgr = new DatabaseManager();
		final String sqlQuery = "INSERT INTO labwork (WorkType, LabworkCost) " +
				"VALUES ('" + workType + "', " + cost + ")";
		
		try{
			dbMgr.runUpdateQuery(sqlQuery);
		} catch(Exception e){
			LOG.severe("A SQL Error occurred while creating labwork");
			e.printStackTrace();
		} finally{
			dbMgr.closeConnection();
		}
		
		LOG.info("Labwork successfully created");
	}
	
	/**
	 * readLabWork method
	 * @param labwork
	
	 * @return LabWork
	 */
	public LabWork readLabWork(LabWork labwork){
		final int id = labwork.getId();
		final DatabaseManager dbMgr = new DatabaseManager();
		final String sqlQuery = "SELECT * FROM labwork WHERE LabworkID = " + id;
		ResultSet rs = null;
		
		try{
			rs = dbMgr.runReadQuery(sqlQuery);
			rs.first();
			final String workType = rs.getString("WorkType");
			final double cost = rs.getDouble("LabworkCost");
			labwork.setWorkType(workType);
			labwork.setCost(cost);
		} catch(Exception e){
			LOG.severe("A SQL Error occurred while reading lab info");
			e.printStackTrace();
		} finally{
			dbMgr.closeConnection(rs);
		}
		LOG.info("Lab info read successfully");
		return labwork;
	}
	
	/**
	 * Method updateLabWork.
	 * @param labwork LabWork
	 */
	public void updateLabWork(LabWork labwork){
		final int id = labwork.getId();
		final String workType = labwork.getWorkType();
		final double cost = labwork.getCost();
		final DatabaseManager dbMgr = new DatabaseManager();
		final String sqlQuery = "UPDATE labwork SET WorkType = '" + workType + 
		        "', LabworkCost = " + cost + " WHERE LabworkID = " + id;
		
		try{
			dbMgr.runUpdateQuery(sqlQuery);
		} catch(Exception e){
			LOG.severe("A SQL Error occurred while updating lab info");
			e.printStackTrace();
		} finally{
			dbMgr.closeConnection();
		}
		
		LOG.info("Lab info updated successfully");
	}
	
	/**
	 * Method deleteLabWork.
	 * @param labwork LabWork
	 */
	public void deleteLabWork(LabWork labwork){
		final int id = labwork.getId();
		final DatabaseManager dbMgr = new DatabaseManager();
		final String sqlQuery = "DELETE FROM labwork WHERE LabworkID = " + id;
		
		try{
			dbMgr.runUpdateQuery(sqlQuery);
		} catch(Exception e){
			LOG.severe("A SQL Exception occurred while deleting lab info");
			e.printStackTrace();
		} finally{
			dbMgr.closeConnection();
		}
		LOG.info("Lab info successfully deleted");
	}
	
	/**
	 * Method readLabWorkID.
	 * @param workType String
	 * @param cost String
	 * @return int
	 */
	public int readLabWorkID(String workType, double cost){
		int id = 0;
		final DatabaseManager dbMgr = new DatabaseManager();
		final String sqlQuery = "SELECT LabworkID FROM labwork WHERE" +
				" WorkType = '" + workType + "' AND LabworkCost = " + cost;
		ResultSet rs = null;
		
		try{
			rs = dbMgr.runReadQuery(sqlQuery);
			rs.first();
			id = rs.getInt("LabworkID");
		} catch(Exception e){
			LOG.severe("A SQL Error occurred while reading lab id");
			e.printStackTrace();
		} finally{
			dbMgr.closeConnection(rs);
		}
		
		LOG.info("Labwork id read successfully read");
		return id;
	}
}
